
namespace Core {
    export class ViewManager extends BaseClass {
        private _views: any;
        private _opens: Array<number>;

        public constructor() {
            super();
            this._views = {};
            this._opens = [];
        }

        public clear(): void {
            this.closeAll();
            this._views = {};
        }

        public getView(key: number): IBaseView {
            return this._views[key];
        }

        public register(key: number, view: IBaseView): void {
            if (view == null) {
                return;
            }
            if (this._views[key]) {
                return;
            }
            this._views[key] = view;
        }

        public unregister(key: number): void {
            if (!this._views[key]) {
                return;
            }
            this._views[key] = null;
            delete this._views[key];
        }

        public destroy(key: number, newView: IBaseView = null): void {
            var oldView: IBaseView = this.getView(key);
            if (oldView) {
                this.unregister(key);
                oldView.destroy();
                oldView = null;
            }
            this.register(key, newView);
        }

        public open(key: number, ...param: any[]): IBaseView {
            var view: IBaseView = this.getView(key);
            if (view == null) {
                console.log("UI_" + key + "不存在");
                return;
            }

            if (view.isShow()) {
                view.open.apply(view, param);
                return view;
            }

            if (view.isInit()) {
                view.addToParent();
                view.open.apply(view, param);
            }
            else {
                App.EasyLoading.showLoading();
                view.loadResource(function () {
                    view.setVisible(false);
                    view.addToParent();
                }.bind(this), function () {
                    view.initUI();
                    view.initData();
                    view.open.apply(view, param);
                    view.setVisible(true);
                    App.EasyLoading.hideLoading();
                }.bind(this));
            }

            this._opens.push(key);
            return view;
        }

        public close(key: number, ...param: any[]): void {
            if (!this.isShow(key)) {
                return;
            }

            var view: IBaseView = this.getView(key);
            if (view == null) {
                return;
            }

            var viewIndex = this._opens.indexOf(key);
            if (key >= 0) {
                this._opens.splice(viewIndex, 1);
            }

            view.removeFromParent();
            view.close.apply(view, param);
        }

        public closeView(view: IBaseView, ...param: any[]): void {
            var keys = Object.keys(this._views);
            for (var i: number = 0, len = keys.length; i < len; i++) {
                var key: number = parseInt(keys[i]);
                if (this._views[key] == view) {
                    this.close(key, param);
                    return;
                }
            }
        }

        public closeAll(): void {
            while (this._opens.length) {
                this.close(this._opens[0]);
            }
        }

        public currOpenNum(): number {
            return this._opens.length;
        }

        public isShow(key: number): boolean {
            return this._opens.indexOf(key) != -1;
        }
    }
}